package com.huatai.vba.finrecon.threadpool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.huatai.vba.common.exception.BusinessException;
import com.huatai.vba.dal.model.VbaFileTask;
import com.huatai.vba.dal.model.VbaFileTaskRecord;
import com.huatai.vba.service.*;

public class ActionRunnable implements Runnable {

	private static final Logger logger = LoggerFactory.getLogger(ActionRunnable.class);

	private String recordId;

	private VbaFileActionService vbaFileActionService;

	private VbaFileTaskService vbaFileTaskService;

	private VbaFileTaskRecordService vbaFileTaskRecordService;

	public ActionRunnable(VbaFileActionService vbaFileActionService, VbaFileTaskService vbaFileTaskService,
			VbaFileTaskRecordService vbaFileTaskRecordService, String recordId) {
		this.vbaFileActionService = vbaFileActionService;
		this.vbaFileTaskService = vbaFileTaskService;
		this.vbaFileTaskRecordService = vbaFileTaskRecordService;
		this.recordId = recordId;
	}

	@Override
	public void run() {
		VbaFileTaskRecord record = null;
		String status = null;
		try {
			record = vbaFileTaskRecordService.queryVbaFileTaskRecord(recordId);
			Integer num = vbaFileTaskRecordService.updateStatus(recordId, "2");
			if (num == 0) {
				return;
			}
			VbaFileTask task = vbaFileTaskService.queryVbaFileTask(record.getTaskId());
			boolean b = vbaFileActionService.executeAction(task.getActionId(), "Auto");
			status = b ? "1" : "8";
		} catch (Exception ex) {
			status = "8";
		}
		try {
			vbaFileTaskRecordService.updateStatus(recordId, status);
		} catch (BusinessException e) {
			logger.error("系统异常", e);
		}

	}

}
